<?php

class payments_model extends MY_Model {

    function __construct() {
        parent::__construct();
        $this->table = 'payments';
    }

    function find_payment_by_id($payment_ID){
        $q = "
            SELECT *
            FROM payments
            WHERE ID = $payment_ID
            ";

        $res = $this->db->query($q)->result_array();
        return $res;
    }

    function find_payments_by_supplier_id($supplier_ID){
        $q = "
            SELECT p.*, orders.supplier_ID as supplier_ID, suppliers.name as supplier_name
            FROM payments p
            LEFT JOIN orders
            ON p.order_ID = orders.ID
            LEFT JOIN suppliers
            ON orders.supplier_ID = suppliers.ID
            WHERE orders.supplier_ID = $supplier_ID
            ";

        $res = $this->db->query($q)->result_array();
        return $res;
    }


    function list_all_payments(){
        $q = "
            SELECT p.*, orders.supplier_ID  as supplier_ID, suppliers.name as supplier_name
            FROM payments p
            LEFT JOIN orders
            ON p.order_ID = orders.ID
            LEFT JOIN suppliers
            ON orders.supplier_ID = suppliers.ID
            WHERE 1
            ";

        $res = $this->db->query($q)->result_array();
        return $res;
    }


    function find_payments_by_order_id($order_ID){
        $q = "
            SELECT p.ID as ID,
            p.payment_date as payment_date,
            p.order_ID as order_ID,
            p.amount as amount,
            p.passed as passed,
            orders.supplier_ID as supplier_ID,
            orders.user_label as user_label,
            suppliers.name as supplier_name
            FROM payments p
            LEFT JOIN orders
            ON p.order_ID = orders.ID
            LEFT JOIN suppliers
            ON orders.supplier_ID = suppliers.ID
            WHERE order_ID = $order_ID
            ";

        $res = $this->db->query($q)->result_array();
        return $res;
    }


    function delete_payment_with_id($payment_ID){
        $this->db->delete($this->table, array('ID' => $payment_ID));
    }
}

?>
